package cn.persistenceCollection.fileBasedImpl

import cn.persistenceCollection.PersistenceContext
import com.easy.server.persistenceCollection.PersistenceSet
import cn.persistenceCollection.Serializer
import cn.persistenceCollection.UnitSerializer
import com.easy.server.persistenceCollection.FileMapperType
import com.easy.server.persistenceCollection.FilePersistenceCollection

/***
 * 本质上是一个FilePersistenceMap<E,Unit>的代理
 */
class FilePersistenceSet<E>(
    filePath: String,
    serializer: Serializer<E>,
    initCap: Int = 1000,
    fileMapperType: FileMapperType = FileMapperType.MergedMemoryMapMapper,
    flushPerOption: Boolean = false,//是否每个操作都及时刷盘
    flushIntervalMills: Long = 1000, //当flushPerOption=false时 定时刷盘的间隔
    context: PersistenceContext = PersistenceContext.default,
) :
    PersistenceSet<E>, FilePersistenceCollection {

    val map = FilePersistenceMap(
        filePath,
        serializer,
        UnitSerializer,
        initCap,
        -1f,
        fileMapperType,
        flushPerOption,
        flushIntervalMills,
        context
    )

    override fun add(element: E): Boolean {
        val contains = map.containsKey(element)
        map[element] = Unit
        return !contains
    }

    override fun remove(element: E): Boolean {
      return  map.remove(element)!=null
    }

    override fun addAll(elements: Collection<E>): Boolean {
        var success = true
        elements.forEach { success = success and add(it) }
        return success
    }

    override fun iterator(): MutableIterator<E> {
        return object : MutableIterator<E> {
            val iterator = map.iterator()
            override fun hasNext(): Boolean {
                return iterator.hasNext()
            }

            override fun next(): E {
                return iterator.next().key
            }

            override fun remove() {
                iterator.remove()
            }
        }
    }


    override fun removeAll(elements: Collection<E>): Boolean {
        var removeAny = false
        elements.forEach {
            removeAny = removeAny or (map.remove(it) != null)
        }
        return removeAny
    }

    override fun retainAll(elements: Collection<E>): Boolean {
        return map.retainAll(elements.map { Pair(it, Unit) })
    }

    override val size: Int
        get() = map.size

    override fun contains(element: E): Boolean {
        return map.containsKey(element)
    }

    override fun containsAll(elements: Collection<E>): Boolean {
        var containsAll = true
        elements.forEach { containsAll = containsAll and map.containsKey(it) }

        return containsAll
    }

    override fun isEmpty(): Boolean {
        return map.isEmpty()
    }

    override fun clear() {
        map.clear()
    }

    override fun flush() {
        map.flush()
    }

    override fun closed(): Boolean {
        return map.closed()
    }

    override fun toString(): String {

        val builder = StringBuilder()
        builder.append("{")

        val iterator = iterator()
        while (iterator.hasNext()) {
            builder.append("${iterator.next()}")

            if (iterator.hasNext()) {
                builder.append(" ,")
            }
        }
        builder.append("}")

        return builder.toString()
    }

    override fun close() {
        map.close()
    }


}